package com.zikao.user.utils;

import com.zikao.user.entity.UserEntity;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * @Author: Likaisheng
 * @Description: 生成激活链接的工具类
 * @Date: Created in 19:16:50 2021-03-06
 * @Modified By:
 */
public class GenerateActivatedLinkUtils {

    private static final String CHECK_CODE = "checkCode";

    public static String generateActivatedLink(UserEntity user) {
        return "点击这里的链接进行激活 http://114.67.94.190/user/user/activate?id="
                + user.getId() + "&checkCode=" + generateCheckcode(user);
    }

    /**
     * 生成校验码，用户密码+UUID唯一标识符，为安全把他们加密发送
     * @return
     */
    public static String generateCheckcode(UserEntity user) {
        ByteSource salt = ByteSource.Util.bytes(user.getUuid());
        SimpleHash simpleHash = new SimpleHash("MD5", user.getPwd(), salt, 4);
        return simpleHash.toString();
    }

    /**
     * 判断校验码是否正确
     * @param user
     * @param checkCode
     * @return
     */
    public static boolean verifyCheckcode(UserEntity user,String checkCode) {
        return generateCheckcode(user).equals(checkCode);
    }
}